import { h } from "vue";
import { staticRouter } from "../router/modules/staticRouter";
import * as icons from "@ant-design/icons-vue";
//菜单扁平化
export const flatRouter = (routerList) => {
  let result = [];
  routerList.forEach((item) => {
    result.push(item);
    if (item.children) {
      result = result.concat(flatRouter(item.children));
    }
  });
  return result;
};

//生成静态路由菜单
export const generateStaticRouterMenu = () => {
  let menuList = [];
  const routerList = staticRouter
    .filter((item) => {
      return item.name === "layout" && item.children.length > 0;
    })
    .map((item) => item.children)
    .flatMap((item) => item);
  menuList = convertRouterToMenu(routerList);
  return menuList;
};
// 动态路由菜单
export const generateDynamicRouterMenu = (routerList) => {
  const menuList = convertRouterToMenu(routerList);
  return menuList;
};

//
export const convertRouterToMenu = (router) => {
  return router.map((item) => {
    const menuItem = {
      key: item.path,
      label: item.meta?.title,
    };

    if (item.meta?.icon) {
      menuItem.icon = () => h(icons[item.meta.icon]);
    }

    if (item.children) {
      menuItem.children = convertRouterToMenu(item.children);
    }
    return menuItem;
  });
};
